﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace UI
{
    internal static class Utilitys
    {
        private static CoroutineController _coroutineController;

        static Utilitys()
        {
            _coroutineController = new GameObject("[Coroutine]").AddComponent<CoroutineController>();
            UnityEngine.Object.DontDestroyOnLoad(_coroutineController.gameObject);
            _coroutineController.gameObject.hideFlags = HideFlags.HideInHierarchy;
        }

        internal static IEnumerator ForWaitCoroutines(List<Coroutine> coroutines)
        {
            for (int i = 0; i < coroutines.Count; i++)
            {
                Coroutine coroutine = coroutines[i];
                yield return coroutine;
            }
        }
        internal static Coroutine StartCoroutine(IEnumerator routine)
        {
            return _coroutineController.StartCoroutine(routine);
        }

        internal static void StopAllCoroutines()
        {
            _coroutineController.StopAllCoroutines();
        }

        internal static void StopCoroutine(IEnumerator routine)
        {
            _coroutineController.StopCoroutine(routine);
        }

        internal static void StopCoroutine(Coroutine routine)
        {
            _coroutineController.StopCoroutine(routine);
        }

        internal static void ForAction<T>(IList<T> list,Action<T> action)
        {
            if (list == null || action == null) return;
            for (int i = 0; i < list.Count; i++)
            {
                action(list[i]);
            }
        }

        internal static void ForAction<T>(T[] list, Action<T> action)
        {
            if (list == null || action == null) return;
            for (int i = 0; i < list.Length; i++)
            {
                action(list[i]);
            }
        }
        internal static void ForAction<T>(IList<T> list, Func<T, bool> func)
        {
            if (list == null || func == null) return;
            for (int i = 0; i < list.Count; i++)
            {
                if (!func(list[i]))
                {
                    return;
                }
            }
        }

        internal static void ForAction<T>(T[] list, Func<T,bool> func)
        {
            if (list == null || func == null) return;
            for (int i = 0; i < list.Length; i++)
            {
                if (!func(list[i]))
                {
                    return;
                }
            }
        }
    }
}
